<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>delay.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_async_delay.js.html">delay.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2007 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Defines a class useful for handling functions that must be
<a name="line17"></a> * invoked after a delay, especially when that delay is frequently restarted.
<a name="line18"></a> * Examples include delaying before displaying a tooltip, menu hysteresis,
<a name="line19"></a> * idle timers, etc.
<a name="line20"></a> * @see ../demos/timers.html
<a name="line21"></a> */
<a name="line22"></a>
<a name="line23"></a>
<a name="line24"></a>goog.provide(&#39;goog.Delay&#39;);
<a name="line25"></a>goog.provide(&#39;goog.async.Delay&#39;);
<a name="line26"></a>
<a name="line27"></a>goog.require(&#39;goog.Disposable&#39;);
<a name="line28"></a>goog.require(&#39;goog.Timer&#39;);
<a name="line29"></a>
<a name="line30"></a>
<a name="line31"></a>
<a name="line32"></a>/**
<a name="line33"></a> * A Delay object invokes the associated function after a specified delay. The
<a name="line34"></a> * interval duration can be specified once in the constructor, or can be defined
<a name="line35"></a> * each time the delay is started. Calling start on an active delay will reset
<a name="line36"></a> * the timer.
<a name="line37"></a> *
<a name="line38"></a> * @param {Function} listener Function to call when the delay completes.
<a name="line39"></a> * @param {number} opt_interval The default length of the invocation delay (in
<a name="line40"></a> *     milliseconds).
<a name="line41"></a> * @param {Object} opt_handler The object scope to invoke the function in.
<a name="line42"></a> * @constructor
<a name="line43"></a> * @extends {goog.Disposable}
<a name="line44"></a> */
<a name="line45"></a>goog.async.Delay = function(listener, opt_interval, opt_handler) {
<a name="line46"></a>  /**
<a name="line47"></a>   * The function that will be invoked after a delay.
<a name="line48"></a>   * @type {Function}
<a name="line49"></a>   * @private
<a name="line50"></a>   */
<a name="line51"></a>  this.listener_ = listener;
<a name="line52"></a>
<a name="line53"></a>  /**
<a name="line54"></a>   * The default amount of time to delay before invoking the callback.
<a name="line55"></a>   * @type {number}
<a name="line56"></a>   * @private
<a name="line57"></a>   */
<a name="line58"></a>  this.interval_ = opt_interval || 0;
<a name="line59"></a>
<a name="line60"></a>  /**
<a name="line61"></a>   * The object context to invoke the callback in.
<a name="line62"></a>   * @type {Object|undefined}
<a name="line63"></a>   * @private
<a name="line64"></a>   */
<a name="line65"></a>  this.handler_ = opt_handler;
<a name="line66"></a>
<a name="line67"></a>
<a name="line68"></a>  /**
<a name="line69"></a>   * Cached callback function invoked when the delay finishes.
<a name="line70"></a>   * @type {Function}
<a name="line71"></a>   * @private
<a name="line72"></a>   */
<a name="line73"></a>  this.callback_ = goog.bind(this.doAction_, this);
<a name="line74"></a>};
<a name="line75"></a>goog.inherits(goog.async.Delay, goog.Disposable);
<a name="line76"></a>
<a name="line77"></a>
<a name="line78"></a>/**
<a name="line79"></a> * A deprecated alias.
<a name="line80"></a> * @deprecated Use goog.async.Delay instead.
<a name="line81"></a> * @constructor
<a name="line82"></a> */
<a name="line83"></a>goog.Delay = goog.async.Delay;
<a name="line84"></a>
<a name="line85"></a>
<a name="line86"></a>/**
<a name="line87"></a> * Identifier of the active delay timeout, or 0 when inactive.
<a name="line88"></a> * @type {number}
<a name="line89"></a> * @private
<a name="line90"></a> */
<a name="line91"></a>goog.async.Delay.prototype.id_ = 0;
<a name="line92"></a>
<a name="line93"></a>
<a name="line94"></a>/**
<a name="line95"></a> * Disposes of the object, cancelling the timeout if it is still outstanding and
<a name="line96"></a> * removing all object references.
<a name="line97"></a> */
<a name="line98"></a>goog.async.Delay.prototype.disposeInternal = function() {
<a name="line99"></a>  goog.async.Delay.superClass_.disposeInternal.call(this);
<a name="line100"></a>  this.stop();
<a name="line101"></a>  delete this.listener_;
<a name="line102"></a>  delete this.handler_;
<a name="line103"></a>};
<a name="line104"></a>
<a name="line105"></a>
<a name="line106"></a>/**
<a name="line107"></a> * Starts the delay timer. The provided listener function will be called after
<a name="line108"></a> * the specified interval. Calling start on an active timer will reset the
<a name="line109"></a> * delay interval.
<a name="line110"></a> * @param {number} opt_interval If specified, overrides the object&#39;s default
<a name="line111"></a> *     interval with this one (in milliseconds).
<a name="line112"></a> */
<a name="line113"></a>goog.async.Delay.prototype.start = function(opt_interval) {
<a name="line114"></a>  this.stop();
<a name="line115"></a>  this.id_ = goog.Timer.callOnce(
<a name="line116"></a>      this.callback_,
<a name="line117"></a>      goog.isDef(opt_interval) ? opt_interval : this.interval_);
<a name="line118"></a>};
<a name="line119"></a>
<a name="line120"></a>
<a name="line121"></a>/**
<a name="line122"></a> * Stops the delay timer if it is active. No action is taken if the timer is not
<a name="line123"></a> * in use.
<a name="line124"></a> */
<a name="line125"></a>goog.async.Delay.prototype.stop = function() {
<a name="line126"></a>  if (this.isActive()) {
<a name="line127"></a>    goog.Timer.clear(this.id_);
<a name="line128"></a>  }
<a name="line129"></a>  this.id_ = 0;
<a name="line130"></a>};
<a name="line131"></a>
<a name="line132"></a>
<a name="line133"></a>/**
<a name="line134"></a> * Fires delay&#39;s action even if timer has already gone off or has not been
<a name="line135"></a> * started yet; guarantees action firing. Stops the delay timer.
<a name="line136"></a> */
<a name="line137"></a>goog.async.Delay.prototype.fire = function() {
<a name="line138"></a>  this.stop();
<a name="line139"></a>  this.doAction_();
<a name="line140"></a>};
<a name="line141"></a>
<a name="line142"></a>
<a name="line143"></a>/**
<a name="line144"></a> * Fires delay&#39;s action only if timer is currently active. Stops the delay
<a name="line145"></a> * timer.
<a name="line146"></a> */
<a name="line147"></a>goog.async.Delay.prototype.fireIfActive = function() {
<a name="line148"></a>  if (this.isActive()) {
<a name="line149"></a>    this.fire();
<a name="line150"></a>  }
<a name="line151"></a>};
<a name="line152"></a>
<a name="line153"></a>
<a name="line154"></a>/**
<a name="line155"></a> * @return {boolean} True if the delay is currently active, false otherwise.
<a name="line156"></a> */
<a name="line157"></a>goog.async.Delay.prototype.isActive = function() {
<a name="line158"></a>  return this.id_ != 0;
<a name="line159"></a>};
<a name="line160"></a>
<a name="line161"></a>
<a name="line162"></a>/**
<a name="line163"></a> * Invokes the callback function after the delay successfully completes.
<a name="line164"></a> * @private
<a name="line165"></a> */
<a name="line166"></a>goog.async.Delay.prototype.doAction_ = function() {
<a name="line167"></a>  this.id_ = 0;
<a name="line168"></a>  if (this.listener_) {
<a name="line169"></a>    this.listener_.call(this.handler_);
<a name="line170"></a>  }
<a name="line171"></a>};
</pre>


</body>
</html>
